package com.zhaolulu.openai;

import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

/**
 * @author zzj
 * @version 1.0
 */
public class CompletionsStreamingDemo {

    public static void main(String[] args) {
        //1. 创建客户端
        var client = OpenAIOkHttpClient.builder()
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build();

        //2. 构造请求参数
        var createParams = ChatCompletionCreateParams.builder()
                // .model(ChatModel.GPT_3_5_TURBO)
                .model("qwen-plus-2025-01-25")
                .addSystemMessage("你是一个java开发助手，名字叫作：火飞鹰")
                .addUserMessage("写一个java的入门示例代码")
                .build();

        //3. 通过客户端，发起请求
        try (var response = client.chat().completions().createStreaming(createParams)) {
            response.stream()
                    .flatMap(chunk -> chunk.choices().stream())
                    .flatMap(choice -> choice.delta().content().stream())
                    .forEach(System.out::print);
        }

    }
}
